package com.moujitx.wecom.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.moujitx.wecom.common.Result;
import com.moujitx.wecom.service.MessageService;

/**
 * 消息控制器，用于处理不同类型的消息发送请求
 */
@RestController
@RequestMapping("/api/message")
public class MessageController {

    @Autowired
    private MessageService messageService;
    
    /**
     * 发送文本消息
     * 
     * @param content 消息内容
     * @param destinationId 目的地ID
     * @return 发送结果
     */
    @PostMapping("/text")
    public Result sendTextMessage(
            @RequestBody(required = false) String content,
            @RequestParam(required = true) Long destinationId) {
        
        if (content == null || content.isEmpty()) {
            content = "(empty content)";
        }
        
        return messageService.sendTextMessage(destinationId, content);
    }
    
    /**
     * 发送Markdown消息
     * 
     * @param content Markdown内容
     * @param destinationId 目的地ID
     * @return 发送结果
     */
    @PostMapping("/markdown")
    public Result sendMarkdownMessage(
            @RequestBody(required = false) String content,
            @RequestParam(required = true) Long destinationId) {
        
        if (content == null || content.isEmpty()) {
            content = "(empty content)";
        }
        
        return messageService.sendMarkdownMessage(destinationId, content);
    }
    
    /**
     * 发送图文消息
     * 
     * @param params 参数，包含title, description, url, picUrl
     * @param destinationId 目的地ID
     * @return 发送结果
     */
    @PostMapping("/news")
    public Result sendNewsMessage(
            @RequestBody Map<String, String> params,
            @RequestParam(required = true) Long destinationId) {
        
        String title = params.getOrDefault("title", "消息通知");
        String description = params.getOrDefault("description", "");
        String url = params.getOrDefault("url", "#");
        String picUrl = params.getOrDefault("picUrl", "");
        
        return messageService.sendNewsMessage(destinationId, title, description, url, picUrl);
    }
    
    /**
     * 发送模板卡片消息
     * 
     * @param params 参数，包含title, description, url
     * @param destinationId 目的地ID
     * @return 发送结果
     */
    @PostMapping("/template_card")
    public Result sendTemplateCardMessage(
            @RequestBody Map<String, String> params,
            @RequestParam(required = true) Long destinationId) {
        
        String title = params.getOrDefault("title", "消息通知");
        String description = params.getOrDefault("description", "");
        String url = params.getOrDefault("url", "#");
        
        return messageService.sendTemplateCardMessage(destinationId, title, description, url);
    }
    
    /**
     * 通用消息发送接口
     * 
     * @param params 参数，包含messageType, content, title, description, url, picUrl
     * @param destinationId 目的地ID
     * @return 发送结果
     */
    @PostMapping
    public Result sendMessage(
            @RequestBody Map<String, String> params,
            @RequestParam(required = true) Long destinationId) {
        
        String messageType = params.getOrDefault("messageType", "text");
        String content = params.getOrDefault("content", "");
        String title = params.getOrDefault("title", "消息通知");
        String description = params.getOrDefault("description", "");
        String url = params.getOrDefault("url", "#");
        String picUrl = params.getOrDefault("picUrl", "");
        
        if (content.isEmpty() && messageType.equals("text")) {
            content = "(empty content)";
        }
        
        return messageService.sendMessage(destinationId, messageType, content, title, description, url, picUrl);
    }
}